package com.course.controller.student;

import com.course.confing.JwtUtils;
import com.course.entity.Result;
import com.course.entity.Student;
import com.course.server.StuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/stu/login")
public class LoginController {
    @Autowired
    private StuService stuService;

    @PostMapping
    public Result login(@RequestBody Student student){
        Student e=stuService.login(student);
        //登录成功，生成令牌，下发令牌
        System.out.println("登录方法被调用");
        if(e!=null){
            Map<String,Object> claims=new HashMap<>();
            claims.put("id",e.getSno());
            claims.put("name",e.getSname());
            String jwt= JwtUtils.generateJwt(claims);
            System.out.println("获取jwt令牌");
            return Result.sucess(jwt);
        }
        return Result.error("账号或者密码错误");
    }
}
